package com.ykg.service.impl;

import com.ykg.dto.EntrustDTO;
import com.ykg.dto.RunStampDTO;
import com.ykg.mapper.EntrustMapper;
import com.ykg.mapper.StampMapper;
import com.ykg.pojo.Entrust;
import com.ykg.pojo.Stamping;
import com.ykg.pojo.UnloadInfo;
import com.ykg.service.IDTOService;
import com.ykg.service.IRunStampService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 类(接口)：
 * Created on 2021/6/25 8:47.
 * Author: zetong
 * Description:
 */
@Service
public class RunStampServiceImpl implements IRunStampService {

    @Autowired
    EntrustMapper entrustMapper;
    @Autowired
    IDTOService idtoService;
    @Autowired
    StampMapper stampMapper;

    @Override//跑跺的委托单列表
    public List<Entrust> getEntrustRun(){
        List<Entrust> entrusts = new ArrayList<Entrust>();
        entrusts.addAll(entrustMapper.getEntrustByState(1));
        entrusts.addAll(entrustMapper.getEntrustByState(2));
        entrusts.addAll(entrustMapper.getEntrustByState(3));
        return entrusts;
    }

    @Override//待跑跺委托单的详细信息
    public EntrustDTO getEntrustNotRunDetailed(String entrustId){
        Entrust entrust = entrustMapper.findByEntrustId(entrustId);
        EntrustDTO entrustDTO = idtoService.createEntrustDTO(entrust);
        return entrustDTO;
    }
    @Override//点击开始跑跺
    public RunStampDTO startRunStamp(String entrustId){
        entrustMapper.updateState(entrustId,2);//开始跑跺
        Entrust entrust = entrustMapper.findByEntrustId(entrustId);
        EntrustDTO entrustDTO = idtoService.createEntrustDTO(entrust);
        float quantity = entrustDTO.getQuantity();
        String listId = entrustDTO.getListId();
        List<Stamping> stampings = stampMapper.findStampAll();
        for (int i = 0;i<8;i++){
            float capacity =  stampings.get(i).getCapacity();
            if(quantity!=0) {
                if (capacity != 0) {
                    if (capacity >= quantity) {
                        capacity = capacity - quantity;
                        stampings.get(i).setCapacity(capacity);
                        stampMapper.updateStamp(stampings.get(i));
                        UnloadInfo unloadInfo = new UnloadInfo(Long.valueOf(i+1),listId,quantity);
                        stampMapper.addUnloadInfo(unloadInfo);
                        quantity = 0;
                    }else{
                        UnloadInfo unloadInfo = new UnloadInfo(Long.valueOf(i+1),listId,capacity);
                        stampMapper.addUnloadInfo(unloadInfo);
                        quantity = quantity-capacity;
                        capacity = 0;
                        stampings.get(i).setCapacity(capacity);
                        stampMapper.updateStamp(stampings.get(i));
                    }
                }
            }else {
                break;
            }
        }
        RunStampDTO runStampDTO = idtoService.createRunStampDTO(entrustDTO);
        return runStampDTO;
    }
    @Override//完成跑跺
    public int finishRunStamp(String entrustId){
        int result = entrustMapper.updateState(entrustId,3);//完成跑跺
        return result;
    }
}
